﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using System.IO;
using System.Diagnostics;
public partial class Shopping_Order_OrderDetail : System.Web.UI.Page
{
    protected string hName = "", hAddress = "", hPhone = "", hEmail = "", hPlaceNm = "", hPlaceID = "", hTaxNo = "";

    int pQuantity = 0;
    decimal pTotal = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Shopping_Order_OrderDetail));
        if (Session["UserID"] == null || Session["IsAdmin"] != "N")
        {
            Session["ErrLogin"] = "B";
            Response.Redirect("~/Member/Login/Login.aspx?pagefile=" + Server.UrlEncode("Order/" + Request.QueryString["KindNo"] + ".aspx"));
            return;
        }
        if (Session["dtBasket"] == null && Session["dtBasketFilm"] == null)
        {
            Response.Redirect(ResolveUrl("~/") + "Cart/" + Request.QueryString["KindNo"] + ".aspx");
        }
        if (!IsPostBack)
        {
            ViewState["Total"] = "0";
            BindPlace();
            BindGuest_Info();
            BindData();
        }
    }
    private void BindData()
    {
        grv_Master.DataSource = GetData();
        grv_Master.DataBind();
    }
    private void BindPlace()
    {
        clsWebShop cls = new clsWebShop();
        ddlPlace.DataSource = cls.Get_PlaceNoneProc();
        ddlPlace.DataTextField = "PlaceNm";
        ddlPlace.DataValueField = "PlaceID";
        ddlPlace.DataBind();
    }
    private void BindGuest_Info()
    {

        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        string UserID = Session["UserID"].ToString();
        msg.CreateFirstNode("UserID", UserID);


        DataTable dt = new DataTable();
        dt = cls.Get_shr_UserByUserID(msg.GetXmlStr.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
        {
            string CusNm = dt.Rows[0]["CusNm"].ToString();
            string Address = dt.Rows[0]["Address"].ToString();
            string Phone = dt.Rows[0]["Phone"].ToString();
            string Email = dt.Rows[0]["Email"].ToString();
            string PlaceNm = dt.Rows[0]["PlaceNm"].ToString();
            string PlaceID = dt.Rows[0]["PlaceID"].ToString();
            clsCommon clsCom = new clsCommon();
            lblBirthDay.Text = clsCom.FormatDate(dt.Rows[0]["BirthDay"].ToString());
            string TaxNo = dt.Rows[0]["TaxNo"].ToString();
            //string 

            txtHoTen.Text = CusNm;
            txtDiaChi.Text = Address;
            txtDienThoai.Text = Phone;
            txtEmail.Text = Email;
            txtTaxNo.Text = TaxNo;
            lblPlace.Text = PlaceNm;
            ddlPlace.Items.FindByValue(PlaceID).Selected = true;

            hName = CusNm;
            hAddress = Address;
            hPhone = Phone;
            hEmail = Email;
            hPlaceNm = PlaceNm;
            hTaxNo = TaxNo;
        }
    }

    private DataTable GetData()
    {
        string sSession = "dtBasket";
        if (Request.QueryString["KindNo"] == "F")
            sSession = "dtBasketFilm";

        DataTable dtBasket = new DataTable();
        DataTable dt = new DataTable();
        if (Session[sSession] != null)
        {
            clsWebShop cls = new clsWebShop();
            lblUSD.Text = "Tỉ giá USD: " + FormatMoney(cls.GetMoney("USD")) + " VNĐ";
            lblUSDHidden.Text = cls.GetMoney("USD").ToString();
            dtBasket = (DataTable)Session[sSession];

            DataRow dr;
            dt.Columns.Add(new DataColumn("PDID"));
            dt.Columns.Add(new DataColumn("PDName"));
            dt.Columns.Add(new DataColumn("Price"));
            dt.Columns.Add(new DataColumn("Quantity"));
            dt.Columns.Add(new DataColumn("Rate"));
            dt.Columns.Add(new DataColumn("KindNo"));
            dt.Columns.Add(new DataColumn("Total"));

            for (int i = 0; i < dtBasket.Rows.Count; i++)
            {
                dr = dt.NewRow();
                dr[0] = dtBasket.Rows[i]["PDID"].ToString();
                dr[1] = dtBasket.Rows[i]["PDNAME"].ToString();
                dr[2] = dtBasket.Rows[i]["PRICE"].ToString();
                dr[3] = dtBasket.Rows[i]["QUANTITY"].ToString();
                dr[4] = dtBasket.Rows[i]["RATE"].ToString();
                dr[5] = dtBasket.Rows[i]["KINDNO"].ToString();
                decimal ThanhTien = int.Parse(dtBasket.Rows[i]["QUANTITY"].ToString()) * decimal.Parse(dtBasket.Rows[i]["PRICE"].ToString());
                if (dtBasket.Rows[i]["RATE"].ToString() == "USD")
                    ThanhTien = ThanhTien * cls.GetMoney("USD");
                dr[6] = ThanhTien.ToString();
                dt.Rows.Add(dr);
            }
        }
        return dt;
    }
    protected void grv_Master_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            pQuantity += int.Parse(DataBinder.Eval(e.Row.DataItem, "Quantity").ToString());
            pTotal += decimal.Parse(DataBinder.Eval(e.Row.DataItem, "Total").ToString());


            Label lblDongia = (Label)e.Row.FindControl("lblDongia");
            lblDongia.Text = FormatMoney(lblDongia.Text);

            Label lblThanhTien = (Label)e.Row.FindControl("lblThanhTien");
            lblThanhTien.Text = FormatMoney(lblThanhTien.Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblsl = new Label();
            Label lbltt = new Label();
            Label lblVND = new Label();
            lblVND.ID = "lblVND";
            lbltt.ID = "lbltt";
            lblsl.ID = "lblSL";
            lblsl.Text = pQuantity.ToString();
            lbltt.ID = "lblTT";
            ViewState["Total"] = pTotal.ToString();
            lbltt.Text = FormatMoney(pTotal.ToString());
            lblVND.Text = " <b>$</b>";
            e.Row.Cells[1].Text = "<b>Tổng cộng</b>";
            e.Row.Cells[1].ColumnSpan = 2;
            e.Row.Cells[3].Controls.Add(lblsl);
            e.Row.Cells[4].Controls.Add(lblVND);
            e.Row.Cells[4].Controls.Add(lbltt);



            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;

            //e.Row.Cells(0).CssClass = "cssBorderGrv";
            //e.Row.Cells(5).CssClass = "cssBorderGrv";
            //e.Row.Cells(6).CssClass = "cssBorderGrv";
            //e.Row.Cells(4).CssClass = "cssBorderGrv";

            e.Row.Cells.RemoveAt(2);
            // e.Row.Cells.RemoveAt(1);
        }
    }
    protected void btnFinish_Click(object sender, EventArgs e)
    {
        string sSession = "dtBasket";
        if (Request.QueryString["KindNo"] == "F")
            sSession = "dtBasketFilm";
        PccMsg msg = new PccMsg();
        clsWebShop cls = new clsWebShop();
        if (chkState.Checked == true)
        {
            msg.CreateFirstNode("UserID", Session["UserID"].ToString());
            msg.CreateFirstNode("AutID", "0");
            msg.CreateFirstNode("CusNm", txtHoTen.Text);
            msg.CreateFirstNode("AccountID", "");
            msg.CreateFirstNode("Address", txtDiaChi.Text);
            msg.CreateFirstNode("PlaceID", ddlPlace.SelectedValue);
            msg.CreateFirstNode("Phone", txtDienThoai.Text);
            msg.CreateFirstNode("Email", txtEmail.Text);
            msg.CreateFirstNode("TaxNo", txtTaxNo.Text.Trim());
            cls.Upd_shr_UserOrder(msg.GetXmlStr.ToString());
        }

        msg = new PccMsg();
        msg.CreateFirstNode("UserID", Session["UserID"].ToString());
        msg.CreateFirstNode("Status", "N");
        msg.CreateFirstNode("Repaid", "0");
        msg.CreateFirstNode("RemainPrice", ViewState["Total"].ToString());
        msg.CreateFirstNode("USD", lblUSDHidden.Text);
        msg.CreateFirstNode("Note_Cus", txtNote.Text);
        msg.CreateFirstNode("Note_MK", "SOLD CUSTOMER");
        msg.CreateFirstNode("UpdID", Session["UserID"].ToString());


        string strReXML = "";
        strReXML = cls.Ins_OrderPD(msg.GetXmlStr.ToString());
        msg = new PccMsg(strReXML);
        string OrderIDMsg = msg.Query("OrderIDMsg");
        if (OrderIDMsg != "")
        {
            try
            {

                DataTable dtBasket = new DataTable();
                if (Session[sSession] != null)
                {
                    dtBasket = (DataTable)Session[sSession];
                    string strSQL = "";
                    decimal Total = 0;
                    for (int i = 0; i < dtBasket.Rows.Count; i++)
                    {
                        string sArrPDID = dtBasket.Rows[i]["PDID"].ToString();
                        string sArrPDPrice = dtBasket.Rows[i]["PRICE"].ToString();
                        string sArrPDQuantity = dtBasket.Rows[i]["QUANTITY"].ToString();
                        string sRate = dtBasket.Rows[i]["RATE"].ToString();
                        decimal ThanhTien = int.Parse(sArrPDQuantity) * decimal.Parse(sArrPDPrice);
                        if (sRate == "USD")
                            ThanhTien = ThanhTien * cls.GetMoney("USD");
                        Total += ThanhTien;
                        strSQL += "    INSERT INTO OrderDetail(OrderID, PDID,Rate, Quantity ,Price,OldPrice, Total,OldTotal,Status,UpdDate ) VALUES('" + OrderIDMsg + "', '" + sArrPDID + "','" + sRate + "'," + sArrPDQuantity + "," + sArrPDPrice.Replace(",", "") + "," + sArrPDPrice.Replace(",", "") + ", " + ThanhTien.ToString() + ", " + ThanhTien.ToString() + ",'N','" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + "')     ";

                    }
                    string strReturnIns = cls.UpdateData(strSQL);
                    //string strReturnUpd = cls.UpdateData("Update OrderPD SET Total=" + Total + " Where OrderID='" + OrderIDMsg + "'");

                    //INSERT FLOWORDERHIST
                    clsFunAp clsFun = new clsFunAp();
                    msg = new PccMsg();
                    msg.CreateFirstNode("OrderID", OrderIDMsg);
                    msg.CreateFirstNode("SignUser", Session["UserID"].ToString());
                    msg.CreateFirstNode("Status", "N");
                    msg.CreateFirstNode("Note", clsFun.NoteTo_N(Total.ToString(), " VNĐ"));

                    cls.Ins_Upd_FlowOrderHist(msg.GetXmlStr);



                    SendMailBasket(OrderIDMsg);

                    ClearSessionShopping();
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.location.href='" + ResolveUrl("~/") + "SuccessfulOrder.aspx'</script>");
                    //Response.Redirect("OrderSuccessful.aspx");
                }

            }
            catch
            {
                cls.UpdateData("Delete from OrderPD Where OrderID='" + OrderIDMsg + "'       Delete from OrderDetail Where OrderID='" + OrderIDMsg + "'");
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Có lỗi tạo đơn đặt hàng')</script>");
            }
        }
    }

    private void SendMailBasket(string OrderID)
    {
        string HTML = "", result = "";
        int SL = 0;
        decimal TT = 0;
        if (grv_Master.Rows.Count > 0)
        {


            HTML += " <table border='0' bordercolor='#407db3' cellpadding='2' cellspacing='0' style='border-collapse: collapse;'   width='100%'>";
            HTML += "             <tr>";
            HTML += "                 <td align='center' colspan='4'>";
            HTML += "                      <table border='0' cellpadding='0' cellspacing='0' style='width: 100%'>";
            HTML += "    <tr>";
            HTML += " <td rowspan='5' style='width: 76px'>";
            HTML += " <img src='" + System.Configuration.ConfigurationManager.AppSettings["AppAddress"] + "/Images/Banner/logo.png' /></td>";
            HTML += " <td align='left'>";
            HTML += " &nbsp;<span id='Label11'  style='ont-family:Tahoma; font-size:13px'>Công ty TNHH Hoàng Nguyên Bảo</span>";
            HTML += " </td>";
            HTML += " </tr>";
            HTML += " <tr>";
            HTML += " <td align='left'>";
            HTML += " &nbsp;<span id='Label18' style='ont-family:Tahoma; font-size:13px'>MST: 0309543650</span></td>";
            HTML += " </tr>";
            HTML += " <tr>";
            HTML += " <td align='left'>";
            HTML += " &nbsp;<span id='Label12' style='ont-family:Tahoma; font-size:13px'>16/83 Nguyễn Thiện Thuật P2 Q3</span></td>";
            HTML += " </tr>";
            HTML += " <tr>";
            HTML += " <td align='left'>";
            HTML += " &nbsp;<span id='Label16' style='ont-family:Tahoma; font-size:13px'>Tel: (08)62909078-62909079 Cel:098.2600917</span></td>";
            HTML += " </tr>";
            HTML += " </table>";
            HTML += " </td>";
            HTML += " </tr>";
            HTML += " <tr>";
            HTML += " <td colspan='4' align='center' valign='top' style='height: 31px'>";
            HTML += " <span id='Label2' style='font-family: Tahoma; font-size: 13pt; font-weight: bold;'>";
            HTML += " CHI TIẾT ĐƠN ĐẶT HÀNG</span></td>";
            HTML += " </tr>";
            HTML += " <tr>";
            HTML += " <td align='right' colspan='4' valign='bottom'>";
            HTML += " <table border='0' cellpadding='0' cellspacing='0'>";
            HTML += " <tr>";
            HTML += " <td align='left'>";
            HTML += " Mã đơn hàng</td>";
            HTML += " <td align='left' valign='middle' style='white-space: nowrap'>";
            HTML += " :&nbsp;<span id='lblMaDH' style='font-weight: bold;'>" + OrderID + "</span></td>";
            HTML += " </tr>";
            HTML += " </table>";
            HTML += " </td>";
            HTML += " </tr>";
            HTML += " <tr>";
            HTML += " <td colspan='4'>";
            HTML += " <div style='border-style:dotted; border-width:1px;'>";
            HTML += " <table id='AutoNumber1' style='border-right: 1px solid; padding-right: 0px; border-top: 1px solid; padding-left: 0px; padding-bottom: 0px; border-left: 1px solid; padding-top: 1px; border-bottom: 1px solid; border-collapse: collapse'    bordercolor='#111111' cellspacing='0'                cellpadding='2' width='100%'>";
            HTML += " <tr>";
            HTML += " <td  nowrap style='padding-right: 4px; padding-left: 4px; padding-bottom: 2px; padding-top: 2px;   border-bottom: 1px dotted; width: 110px;' align='left' nowrap>";
            HTML += " Tên khách hàng</td>";
            HTML += " <td align='left' colspan='3' nowrap='nowrap' style='padding-right: 4px; padding-left: 4px;     padding-bottom: 1px; padding-top: 1px; border-bottom: 1px dotted'>";
            HTML += " :&nbsp;<span id='lblTenKH'>" + txtHoTen.Text + "</span></td>";
            HTML += " </tr>";
            HTML += " <tr>";
            HTML += " <td nowrap style='padding-right: 4px; padding-left: 4px; padding-bottom: 2px; padding-top: 2px; border-bottom: 1px dotted; width: 95px;' align='left'>";
            HTML += " Địa chỉ</td>";
            HTML += " <td nowrap style='padding-right: 4px; padding-left: 4px; padding-bottom: 2px; padding-top: 2px; border-bottom: 1px dotted' align='left'>";
            HTML += " :&nbsp;<span id='lblDiaChi'>" + txtDiaChi.Text + "</span></td>";
            HTML += " <td nowrap style='padding-right: 4px; padding-left: 4px; padding-bottom: 2px; padding-top: 2px;         border-bottom: 1px dotted; width: 114px;' align='left'>";
            HTML += " Tỉnh, Thành Phố</td>";
            HTML += " <td nowrap style='padding-right: 4px; padding-left: 4px; padding-bottom: 2px; padding-top: 2px;   border-bottom: 1px dotted' align='left'>";
            HTML += " :&nbsp;<span id='lblNoichon'>" + ddlPlace.SelectedItem.Text + "</span></td>";
            HTML += " </tr>";
            if (txtTaxNo.Text.Trim() != "")
            {
                HTML += " <tr>";
                HTML += " <td nowrap style='padding-right: 4px; padding-left: 4px; padding-bottom: 2px; padding-top: 2px;   border-bottom: 1px dotted; width: 95px;' align='left'>";
                HTML += " Mã số thuế</td>";
                HTML += " <td align='left' colspan='3' nowrap='nowrap' style='padding-right: 4px; padding-left: 4px;     padding-bottom: 1px; padding-top: 1px; border-bottom: 1px dotted'>";
                HTML += " :&nbsp;<span id='lblTenKH'>" + txtTaxNo.Text + "</span></td>";
                HTML += " </tr>";
            }

            HTML += " <tr>";
            HTML += " <td nowrap style='padding-right: 4px; padding-left: 4px; padding-bottom: 2px; padding-top: 2px;   width: 95px;' align='left'>";
            HTML += " Điện thoại</td>";
            HTML += " <td nowrap style='padding-right: 4px; padding-left: 4px; padding-bottom: 2px; padding-top: 2px;   align='left'>";
            HTML += " :&nbsp;<span id='lblSDT'>" + txtDienThoai.Text + " </span>";
            HTML += " </td>";
            HTML += " <td nowrap style='padding-right: 4px; padding-left: 4px; padding-bottom: 2px; padding-top: 2px;    width: 114px;' align='left'>";
            HTML += " Email</td>";
            HTML += " <td nowrap style='padding-right: 4px; padding-left: 4px; padding-bottom: 2px; padding-top: 2px;    align='left'>";
            HTML += " :&nbsp;<span id='lblEmail'>" + txtEmail.Text + "</span></td>";
            HTML += " </tr>";
            HTML += " </table>";
            HTML += " </td>";
            HTML += " </tr>";
            HTML += "   <tr>";
            HTML += "  <td>";
            HTML += "     </td>";
            HTML += "    <td>";
            HTML += "    </td>";
            HTML += " <td>";
            HTML += "  </td>";
            HTML += "   <td>";
            HTML += "  </td>";
            HTML += "  </tr>";

            HTML += "   </table>";
            HTML += "   </div>";

            //================================================= danh sach san pham
            HTML += " <div width='100%' align='right'>";
            HTML += lblUSD.Text;
            HTML += "<table border='1' cellpadding='2' cellspacing='0' style='width: 100%;border-collapse:collapse' bordercolor='#407db3'>";
            //Header
            HTML += "<tr height='30px' style='BACKGROUND-COLOR: #D8ECFF;font-family:Tahoma;font-size:13px;'>";
            HTML += "<th width='80px'   nowrap>" + ((System.Web.UI.WebControls.DataControlFieldCell)(grv_Master.Rows[0].Cells[0])).ContainingField.HeaderText + "</th>";
            HTML += "<th align='left'  nowrap>" + ((System.Web.UI.WebControls.DataControlFieldCell)(grv_Master.Rows[0].Cells[1])).ContainingField.HeaderText + "</th>";
            HTML += "<th  nowrap>" + ((System.Web.UI.WebControls.DataControlFieldCell)(grv_Master.Rows[0].Cells[2])).ContainingField.HeaderText + "</th>";
            HTML += "<th  nowrap>" + ((System.Web.UI.WebControls.DataControlFieldCell)(grv_Master.Rows[0].Cells[3])).ContainingField.HeaderText + "</th>";
            HTML += "<th  nowrap>" + ((System.Web.UI.WebControls.DataControlFieldCell)(grv_Master.Rows[0].Cells[4])).ContainingField.HeaderText + "</th>";
            HTML += "</tr>";
            //Row
            for (int i = 0; i < grv_Master.Rows.Count; i++)
            {
                if (i % 2 == 0)
                    HTML += "<tr style='BACKGROUND-COLOR: #feffeb;font-family:Tahoma;font-size:12px;'>";
                else
                    HTML += "<tr style='BACKGROUND-COLOR: #ffffff;font-family:Tahoma;font-size:12px;'>";

                Label lblMaSP = (Label)grv_Master.Rows[i].FindControl("lblMaSP");
                Label lblTenSP = (Label)grv_Master.Rows[i].FindControl("lblTenSP");
                Label lblDonGia = (Label)grv_Master.Rows[i].FindControl("lblDonGia");
                Label lblSoLuong = (Label)grv_Master.Rows[i].FindControl("lblSoLuong");
                Label lblThanhTien = (Label)grv_Master.Rows[i].FindControl("lblThanhTien");
                Label lblKindMoney = (Label)grv_Master.Rows[i].FindControl("lblKindMoney");


                HTML += "<td  nowrap><a  href='" + System.Configuration.ConfigurationManager.AppSettings["AppAddress"].ToString() + "/Shopping/Stall/ProductDetail.aspx?PDID=" + lblMaSP.Text + "'>" + lblMaSP.Text + "</a></td>";
                HTML += "<td  nowrap>" + lblTenSP.Text + "</td>";
                HTML += "<td align='right'  nowrap>" + lblDonGia.Text + " " + lblKindMoney.Text + "</td>";
                HTML += "<td align='right'  nowrap>" + lblSoLuong.Text + "</td>";
                HTML += "<td align='right'  nowrap>" + lblThanhTien.Text + "</td>";
                SL += int.Parse(lblSoLuong.Text);
                TT += decimal.Parse(lblThanhTien.Text);

                HTML += "</tr>";
            }
            //Footer
            HTML += "<tr height='30px' style='BACKGROUND-COLOR: orange;font-family:Tahoma;font-size:13px;'>";


            Label lblSL = (Label)grv_Master.Rows[grv_Master.Rows.Count - 1].FindControl("lblSL");
            Label lbltt = (Label)grv_Master.Rows[grv_Master.Rows.Count - 1].FindControl("lbltt");
            Label lblVND = (Label)grv_Master.Rows[grv_Master.Rows.Count - 1].FindControl("lblVND");

            HTML += "<th colspan='3' align='left'><b>Tổng cộng</b></th>";
            HTML += "<th align='right'>" + SL.ToString() + "</th>";
            HTML += "<th align='right'><b>$</b> " + FormatMoney(TT).ToString() + "</th>";

            /*HTML += "<td>" + ((System.Web.UI.WebControls.DataControlFieldCell)(grv_Master.Rows[grv_Master.Rows.Count-1].Cells[0])).ContainingField.FooterText + "</td>";
            HTML += "<td>" + ((System.Web.UI.WebControls.DataControlFieldCell)(grv_Master.Rows[grv_Master.Rows.Count - 1].Cells[1])).ContainingField.FooterText + "</td>";
            HTML += "<td>" + ((System.Web.UI.WebControls.DataControlFieldCell)(grv_Master.Rows[grv_Master.Rows.Count - 1].Cells[2])).ContainingField.FooterText + "</td>";
            HTML += "<td>" + ((System.Web.UI.WebControls.DataControlFieldCell)(grv_Master.Rows[grv_Master.Rows.Count - 1].Cells[3])).ContainingField.FooterText + "</td>";
            HTML += "<td>" + ((System.Web.UI.WebControls.DataControlFieldCell)(grv_Master.Rows[grv_Master.Rows.Count - 1].Cells[4])).ContainingField.FooterText + "</td>";
            HTML += "</tr>";*/


            HTML += "</table>";
            HTML += "</div>";

            if (txtNote.Text.Trim() != "")
            {
                HTML += "<br>"; HTML += "<br>";
                HTML += " <table border='0' cellpadding='0' cellspacing='0' style='width: 100%'>     ";
                HTML += "  <tr>";
                HTML += "        <td style='width: 5%; white-space: nowrap'>";
                HTML += "           Ghi chú: </td>";
                HTML += "      <td>";
                HTML += txtNote.Text.Trim();
                HTML += " </td>";
                HTML += "  </tr>";
                HTML += " </table>";
            }
            HTML += "<br>";
            HTML += "Thank you to  my website: <a  href='http://" + System.Configuration.ConfigurationManager.AppSettings["AppAddress"].ToString() + "'>" + System.Configuration.ConfigurationManager.AppSettings["AppAddress"].ToString() + "</a>";

            clsCommon clsCom = new clsCommon();
            string urlAttach = "";
            if (Request.QueryString["KindNo"] == "F")
            {
                urlAttach = ExportExcel();

            }

            result = clsCom.Send_AppEmail(System.Configuration.ConfigurationManager.AppSettings["SaleMail"].ToString(), txtEmail.Text, System.Configuration.ConfigurationManager.AppSettings["SaleMail"].ToString() + ";" + System.Configuration.ConfigurationManager.AppSettings["WebMail"].ToString(), urlAttach, "Chi tiet Don Dat hang(" + System.DateTime.Now.ToString("dd/MM/yyyy  hh:mm:ss") + ")", HTML);
            try
            {               
                File.Delete(urlAttach);
            }
            catch { }
        }

    }

    
    private string ExportExcel()
    {
        StreamWriter writer = null;
        FileStream outputfile = null;
        string FilePathName = "";
        try
        {
            string DocFileName = "ListFilm-" + System.DateTime.Now.ToString("ddMMyyyyhhmmss") + ".xls";
            FilePathName = Server.MapPath("..//") + "\\Database\\ListFilm\\" + DocFileName;
            try
            {
                File.Delete(FilePathName);
            }
            catch { }

            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            PrintTable().RenderControl(hw);
            string HtmlInfo = tw.ToString().Trim();

            outputfile = new FileStream(FilePathName, FileMode.Create, FileAccess.Write);
            writer = new StreamWriter(outputfile, System.Text.Encoding.GetEncoding("utf-8"));
            writer.BaseStream.Seek(0, SeekOrigin.End);
            outputfile.Flush();
            writer.WriteLine(HtmlInfo);
            writer.Flush();
            writer.Close();


        }
        catch (Exception)
        {
            FilePathName = "";
        }
        return FilePathName;
    }

    private Table PrintTable()
    {
        clsWebShop cls = new clsWebShop();
        Table tbl = new Table();
        DataTable dt = (DataTable)Session["dtBasketFilm"];

        string Css = "tbl2_trHeader";
        TableRow row = new TableRow();
        CreateRow(row, "<b>Mã số</b>", Css);
        CreateRow(row, "<b>Tên phim</b>", Css);
        CreateRow(row, "<b>Dung lượng</b>", Css);
        tbl.Rows.Add(row);

        for (int i = 0; i < dt.Rows.Count; i++)
        {

            string MaSo = "";
            PccMsg msg = new PccMsg();
            msg.CreateFirstNode("PDID", dt.Rows[i]["PDID"].ToString());
            DataTable dtPr = cls.Get_pd_PropertyDetailByCondition_NonePro(msg.GetXmlStr).Tables[0];
            if (dtPr.Rows.Count > 0)
            {
                MaSo = dtPr.Rows[0]["PrtdDesc"].ToString();
            }
            Css = i % 2 == 0 ? "tbl2_trRow" : "";

            row = new TableRow();
            CreateRow(row, MaSo, Css);
            CreateRow(row, dt.Rows[i]["PDNAME"].ToString(), Css);
            CreateRow(row, dt.Rows[i]["CAPACITY"].ToString(), Css);
            tbl.Rows.Add(row);
        }

        return tbl;

    }
    private void CreateRow(TableRow row, string sText, string Css)
    {
        TableCell cell = new TableCell();
        cell.Text = sText;
        cell.CssClass = Css;
        cell.BorderWidth = 1;
        row.Cells.Add(cell);
    }



    private string GetControlRenderString(System.Web.UI.Control ctrl)
    {
        string ret = "";
        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
        try
        {
            HtmlForm frm = new HtmlForm();
            frm.Controls.Add(ctrl);
            ctrl.RenderControl(htmlWriter);
            ret = stringWriter.ToString().Replace("\n", "").Replace("\t", "").Replace("\r", "");
        }
        catch (Exception e)
        {
            ret = e.Message;
        }
        return ret;
    }



    private void ClearSessionShopping()
    {

        string sSession = "dtBasket";
        if (Request.QueryString["KindNo"] == "F")
            sSession = "dtBasketFilm";
        Session[sSession] = null;

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Response.Redirect("../Basket/Basket.aspx");
        Response.Redirect(ResolveUrl("~/") + "Cart/" + Request.QueryString["KindNo"] + ".aspx");
    }

    private string FormatMoney(string sNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", decimal.Parse(sNum));
        }
        catch (Exception) { }
        return sReturn;
    }


    private string FormatMoney(decimal dNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", dNum);
        }
        catch (Exception) { }
        return sReturn;
    }
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public string AjaxCheckEmail(string value)
    {
        string flag = "no";
        clsWebShop cls = new clsWebShop();
        DataTable dt = new DataTable();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("ExcUserID", Session["UserID"].ToString());
        msg.CreateFirstNode("Email", value.Trim());
        dt = cls.Get_shr_UserByUserEmail(msg.GetXmlStr.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
            flag = "yes";
        return flag;

    }


}
